<template>
	<div class="card-page">
		<tool-bar nav="" :title='action_name + " YApi用户"'>
			<el-button size="small" @click="$router.push('/dev/product-line/add')" v-if="$props.action=='edit'">新建</el-button>
			<el-button size="small" @click="set_pwd_click" v-if="info.id"> 设置密码</el-button>
			<el-button size="small" @click="save_click" type="primary"> 保存</el-button>
		</tool-bar>
		<el-row :gutter="12" type="flex">
			<el-col :span="12">
				<el-card shadow="always">
                    <template #header> <span>基本信息</span> </template>
					<kv label="姓名">
						<el-input v-model="info.username" chk="@chk_cn_name"/>
					</kv>

					<kv label="邮箱">
						<el-input v-model="info.email" chk="email"/>
					</kv>
				</el-card>
			</el-col>

		</el-row>
	</div>
</template>

<script setup>
/**
 * Created by CodeGenerator  at 2021-04-09 13:44:04
 */





//定义 vue2的this,当前组件
const $self = getCurrentInstance();

//定义 $attrs
const $attrs = useAttrs();



//定义data
const $data = reactive({"password":"","info":{}});
const info = $computed(()=> $data.info );

const $props = defineProps({
    action: { type: String } ,
    id : { type: String }
});




const list = ref();

//定义事件
const $emit = defineEmits(["update:modelValue","loaded"]);

const action = $computed( function() {
    return $props.id ? "edit" : "add";
});
const action_name = $computed( function() {
    return {add: "添加", edit: "修改"}[$props.action]
});

const chk_cn_name = function(value) {
    if (value.length < 2) return '长度>=2';
    if (value.includes(' ')) return '不能包含空格';
    var reg = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>《》/?~！@#￥……&*（）——|{}【】‘；：”\"\\\\/。，、？ ]");
    if (reg.test(value)) {
        return "不能包含特殊字符"
    }
};
const loadData = function() {
    if (!$props.id) return;
    jv.$http.post("/dev/yapi-user/detail/" + $props.id).then(res => {
        $data.info = res.data.data;
    });
};
const save_click = function() {
//校验
    if (jv.chk_main() == false) {
        return;
    }
    jv.$http.post("/dev/yapi-user/save", $data.info).then(res => {
//[axios拦截器中已处理了异常]。
        jv.info(action_name + " 成功");
        jv.$router.push("/dev/yapi-user/edit/" + res.data.data)
    })
};
const set_pwd_click = function() {
    jv.prompt("输入密码").then(res => {
        jv.$http.post("/dev/yapi-user/set-pwd/" + $data.info.id, {password: res.value})
            .then(res => {
                jv.alert("修改成功！")
            });
    });
};

onMounted( function() {
    loadData();
} )



</script>
